Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

ArrayDeque → ArrayDeque in Java

ArrayDeque

ArrayDeque in Java

ArrayDeque in Queue

The ArrayDeque class in Java implements a growable double-ended queue (deque) based on an array. It offers efficient insertion and removal operations at both ends (front and back) of the queue. characteristics of ArrayDeque Double-ended: Elements can be added and removed from both the front (head) and the back (tail) of the queue. Resizable array: The underlying array automatically grows or shrinks as needed to accommodate insertions and removals. Efficient operations: Adding and removing elements from either end generally have constant average time complexity (O(1)).

Initializing and Declaring ArrayDeque

Similar to other collection classes, declaring an ArrayDeque involves creating a reference variable. Declaration
Syntax ArrayDeque<DataType> dequeName;
ArrayDeque: This specifies the class you're using. <DataType>: Placeholder for the data type the deque will store. dequeName: The name for your reference variable.
Initialization 1. Creating an empty ArrayDeque
Creating empty arraydequeue ArrayDeque<String> messages = new ArrayDeque<>();

2. Creating an ArrayDeque with initial elements (Collection initializer):
ArrayDequeue with initial elements ArrayDeque<Integer> numbers = new ArrayDeque<>() {{ add(10); add(20); add(5); }};

Adding and Removing Elements

addFirst(E element): Adds an element at the front of the deque. ⯁ addLast(E element): Adds an element at the back of the deque. ⯁ pollFirst(): Retrieves and removes the element from the front of the deque, or returns null if empty. ⯁ pollLast(): Retrieves and removes the element from the back of the deque, or returns null if empty. ⯁ offerFirst(E element): Inserts an element at the front of the deque (similar to addFirst but may not throw an exception if full). ⯁ offerLast(E element): Inserts an element at the back of the deque (similar to addLast but may not throw an exception if full). ⯁ removeFirst(): Removes and returns the element from the front of the deque (throws an exception if empty). ⯁ removeLast(): Removes and returns the element from the back of the deque (throws an exception if empty).

Deque vs. Queue

ArrayDeque is a deque, while the standard Queue interface only specifies first-in-first-out (FIFO) behavior (adding at the back and removing from the front). ArrayDeque provides the flexibility to add and remove elements from both ends, making it more versatile.

ArrayDeque Examples

Here are a few examples of using ArrayDeque in Java:

1. String Deque

ArrayDequeue example with string elements in java import java.util.ArrayDeque; public class Main { public static void main(String[] args) { ArrayDeque<String> messages = new ArrayDeque<>(); messages.addFirst("Hello"); messages.addLast("How"); messages.addLast("are"); messages.addLast("you?"); System.out.println("Deque elements: " + messages); while (!messages.isEmpty()) { System.out.println(messages.pollFirst()); // Removes and returns first element } } }

Output

Deque elements: [Hello, How, are, you?] Hello How are you?
This example creates a deque of strings, adds elements to both ends, and then iterates through them using pollFirst which removes and returns the first element.

2. Integer Deque

Java ArrayDeque example import java.util.ArrayDeque; public class Main { public static void main(String[] args) { ArrayDeque<Integer> numbers = new ArrayDeque<>(); numbers.offerFirst(10); // Similar to addFirst, but returns false if full numbers.offerLast(20); numbers.offer(30); // Adds to the end (equivalent to addLast) System.out.println("Deque elements: " + numbers); for (int num : numbers) { System.out.println(num); // Iterating using enhanced for loop } } }

Output

Deque elements: [10, 20, 30] 10 20 30
This example demonstrates an integer deque. It uses offer methods which return a boolean instead of throwing exceptions on a full deque. It also showcases iterating through the deque elements using an enhanced for loop.

3. Custom Object Deque

ArrayDeque example with object as elements in Java import java.util.ArrayDeque; class Product { String name; double price; public Product(String name, double price){ this.name=name; this.price=price; } } public class Main { public static void main(String[] args) { ArrayDeque<Product> cart = new ArrayDeque<>(); cart.add(new Product("Shirt", 19.99)); cart.add(new Product("Shoes", 49.95)); System.out.println("Cart items:"); for (Product product : cart) { System.out.println(product.name + " ($ " + product.price + ")"); } } }

Output

Cart items: Shirt ($ 19.99) Shoes ($ 49.95)
This example creates a deque to hold custom Product objects. It demonstrates adding objects and iterating through them using a for-each loop to access product details.
Note : ArrayDeque can be used with any data type as long as the elements can be stored in memory. These examples are just for your reference, you can use any datatype.

Tutorials